1 package uba.db.ar;
2
3 import java.util.*;
4
5 /***
6 * Es la definicion de los atributos de una tupla
7 *
8 * @version 1.0
9 *
10 */
11
12 public class TuplaDef {
13
14 private List def;
15
16 private Map nameToIndex = new HashMap();
17
18 public TuplaDef() {
19 this(new ArrayList());
20 }
21
22 public TuplaDef(List attributeDefinitions) {
23 def = new ArrayList();
24 Iterator it = attributeDefinitions.iterator();
25 while (it.hasNext()) {
26 AttributeDef eachDef = (AttributeDef) it.next();
27 addDefinition(eachDef);
28 }
29 def = attributeDefinitions;
30 }
31
32 /***
33 * addDef
34 *
35 * @param attributeDef
36 * AttributeDef
37 */
38
39 public void addDefinition(AttributeDef attributeDef) {
40 def.add(attributeDef);
41 /*** @todo falta chequear que el nombre no exista previamente */
42 nameToIndex.put(attributeDef.getName(), new Integer(def.size()));
43 }
44
45 public AttributeDef attributeDefAt(String name) {
46 return attributeDefAt(indexOf(name));
47 }
48
49 public int indexOf(String name) {
50 /*** @todo Untested, y falta chequear que el nombre exista */
51 int indice;
52 indice = ((Integer) nameToIndex.get(name)).intValue();
53 return indice;
54 }
55
56 public AttributeDef attributeDefAt(Integer indice) {
57 return attributeDefAt(indice.intValue());
58 }
59
60 public AttributeDef attributeDefAt(int indice) {
61 indice--;
62 return ((AttributeDef) def.get(indice));
63 }
64
65 public int getSize() {
66 return def.size();
67 }
68
69 /***
70 * newFromTupla Devuelve una nueva tupla definition a partir de una lista de
71 * Integers indicando cuales atributos copiar
72 *
73 * @param listaIndices
74 * List
75 * @return TuplaDef
76 */
77
78 public TuplaDef newFromListOfIndexes(List listaIndices) {
79 TuplaDef newDef = new TuplaDef();
80 Iterator i = listaIndices.iterator();
81 Integer indice;
82 while (i.hasNext()) {
83 indice = (Integer) i.next();
84 newDef.addDefinition(attributeDefAt(indice.intValue()));
85 }
86 return newDef;
87 }
88
89 public String toString() {
90 Iterator i = def.iterator();
91 Integer indice;
92 String result = "", tmp;
93 result = "TuplaDef: [";
94 while (i.hasNext()) {
95
96
97 result = result + " " + i.next().toString();
98 }
99 result = result + "]";
100 return result;
101 }
102
103 public List atributos() {
104 /*** @todo esto no se ve bien, pero bueno */
105 return def;
106 }
107
108 public TuplaDef concat(TuplaDef tuplaDefRight) {
109 TuplaDef nuevaDef = new TuplaDef();
110 Iterator i;
111
112 i = atributos().iterator();
113 while (i.hasNext()) {
114 nuevaDef.addDefinition((AttributeDef) i.next());
115 }
116
117 /*** @todo falta renombrar los campos que puedan estar repetidos!!! */
118 i = tuplaDefRight.atributos().iterator();
119 while (i.hasNext()) {
120 nuevaDef.addDefinition((AttributeDef) i.next());
121 }
122 return nuevaDef;
123 }
124 }